package com.longteng.diamond.controller;


import com.longteng.diamond.domain.ProjectShareNode;
import com.longteng.diamond.factory.ProjectShareFactory;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.service.ProjectShareService;
import com.longteng.diamond.service.UserService;
import com.longteng.diamond.service.impl.ProjectHostInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/7/22
 * TIME : 23:16
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@RequestMapping("/projectShareController")
@Controller
@Slf4j
public class ProjectShareController {
    /**
     * 成员列表页面
     */
    private final static String PROJECTMEMBERPAGE ="ProjectMemberPage";

    @Autowired
    ProjectHostInterface projectHostInterface;
    @Autowired
    ProjectShareFactory projectShareFactory;

    @Autowired
    ProjectShareService projectShareService;
    @Autowired
    UserService userService;
    /**
     * 编辑项目:模块、host 页面
     * @param projectShareNode
     * @return ModelAndView
     */
    @RequestMapping("/editProjectSharePage")
    public ModelAndView editProjectSharePage(ProjectShareNode projectShareNode){
        ModelAndView modelAndView = new ModelAndView();
        String pageName =projectShareNode.getPageName();
        modelAndView.setViewName("page/project/edit"+pageName);
        if (null != projectShareNode.getId() && projectShareNode.getId() > 0) {
            projectShareNode = projectShareService.findProjectShareNodeById(projectShareNode);
        }
        /**如果是成员列表新增编辑页，调用所有用户列表返回**/
        if(null!=pageName&&pageName.equalsIgnoreCase(PROJECTMEMBERPAGE)){
            modelAndView.addObject("userList",userService.getAllUser());
        }
        modelAndView.addObject("projectShareNode", projectShareNode);
        modelAndView.addObject("projectId",projectShareNode.getProjectId());
        return modelAndView;
    }
    /**
     * 修改项目:模块、host
     * @param projectShareNode
     * @return ModelAndView
     */
    @RequestMapping("/updateProjectShare")
    public ModelAndView updateProjectShare(ProjectShareNode projectShareNode){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("shareType",projectShareNode.getShareType());
        modelAndView.addObject("projectId",projectShareNode.getProjectId());
        modelAndView.setViewName("/page/project/edit"+projectShareNode.getPageName());
        RequestResult requestResult;
        try {
            requestResult = projectShareFactory.projectShareInterface(projectShareNode.getShareType()).updateProjectShare(projectShareNode);
        }catch (Exception e){
            log.error("修改错误",e);
            modelAndView.addObject("errorMsg","修改错误,请联系管理员");
            return modelAndView;
        }
        if(!requestResult.isSuccess()){
            modelAndView.addObject("errorMsg",requestResult.getMsg());
            return modelAndView;
        }
        modelAndView.setViewName("page/project/projectSharePage");
        return modelAndView;
    }
    /**
     * 新增项目:模块、host
     * @param projectShareNode
     * @return ModelAndView
     */
    @RequestMapping("/addProjectShare")
    public ModelAndView addProjectShare(ProjectShareNode projectShareNode){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("shareType",projectShareNode.getShareType());
        modelAndView.addObject("projectId",projectShareNode.getProjectId());
        modelAndView.setViewName("/page/project/edit"+projectShareNode.getPageName());
        RequestResult requestResult;
        try {
             requestResult = projectShareFactory.projectShareInterface(projectShareNode.getShareType()).addProjectShare(projectShareNode);
        }catch (Exception e){
            log.error("新增错误",e);
            modelAndView.addObject("errorMsg","新增错误,请联系管理员");
            return modelAndView;
        }
        if(!requestResult.isSuccess()){
            modelAndView.addObject("errorMsg",requestResult.getMsg());
            return modelAndView;
        }
        modelAndView.setViewName("page/project/projectSharePage");
        return modelAndView;
    }
    /**
     * 项目:模块、host、成员列表
     * @param shareType
     * @return ModelAndView
     */
    @RequestMapping("/projectSharePage")
    public ModelAndView projectSharePage(Integer shareType,Integer projectId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("page/project/projectSharePage");
        modelAndView.addObject("shareType",shareType);
        modelAndView.addObject("projectId",projectId);
        return modelAndView;
    }
    /**
     * 删除项目:模块、host、成员列表
     * @param id
     * @return RequestResult
     */
    @RequestMapping("/deleteProjectShare")
    public @ResponseBody RequestResult deleteProjectShare(Integer shareType,Integer id){
        RequestResult result=new RequestResult();
        try{
             projectShareService.deleteProjectShare(shareType,id);
             result.setSuccess(true);
        }catch (Exception e){
            log.error("删除异常!",e);
            result.setSuccess(false);
            result.setMsg("删除异常,联系管理员!");
        }
        return result;
    }
    /**
     * 删除项目:模块、host、成员列表
     * @param id
     * @return RequestResult
     */
    @RequestMapping("/deleteProjectShareCheck")
    public @ResponseBody RequestResult deleteProjectShareCheck(Integer shareType,Integer id){
        RequestResult result=new RequestResult();
        try{
            result= projectShareService.deleteProjectShareCheck(shareType,id);
        }catch (Exception e){
            log.error("删除异常!",e);
            result.setSuccess(false);
            result.setMsg("删除异常,联系管理员!");
        }
        return result;
    }
    /**
     * 项目:模块、host、成员列表数据
     * @param projectShareNode
     * @return TablePage
     */
    @RequestMapping("/getTableData")
    public @ResponseBody TablePage getTableData(ProjectShareNode projectShareNode){
        TablePage tablePage = new TablePage();
        try{
           tablePage= projectShareService.getTableData(projectShareNode);
        }catch (Exception e){
            log.error("获取数据异常,请联系管理员",e);
        }

        return tablePage;
    }
//    /**
//     * 获取host为接口绑定
//     * @param projectId
//     * @return ModelAndView
//     */
//    @RequestMapping("/getHostForInterface")
//    public @ResponseBody List<Map<String,String>> getHostForInterface(Integer projectId){
//      return  projectHostInterface.getHostForInterface(projectId);
//    }
}
